package com.lyc.servlet.admin;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyc.dao.MedalInfoDAO;
import com.lyc.dao.imp.MedalInfoDAOImpl;
import com.lyc.entity.MedalInfo;

@WebServlet("/admin/medal-add.let")
public class MedalAddServlet extends HttpServlet {

	private MedalInfoDAO dao = new MedalInfoDAOImpl();
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.reset();
		response.setContentType("application/json;charset=UTF-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        
        try {
        	MedalInfo e = new MedalInfo();
        	e.setMedalId(null);
        	e.setSeId(Integer.parseInt(request.getParameter("seId")));
        	e.setSportsmanId(Integer.parseInt(request.getParameter("sportsmanId")));
        	e.setMedalNum(Integer.parseInt(request.getParameter("medalNum")));
        	e.setMedalResult(request.getParameter("medalResult"));
        	e.setMedalDate(sdf.parse(request.getParameter("medalDate")));
        	
            try{
                dao.insert(e);
                response.getWriter().print("{\"ok\":true}");
            }catch (Exception ex){
                throw new ServletException("获奖编号不能重复", ex);
            }
        }catch(Exception e) {
        	response.getWriter().print("{\"ok\":false,\"msg\":\""+e.getMessage()+"\"}");
        }
        response.flushBuffer();
	}
}
